package com.sinodata.bsm.center.service.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.bean.Condition;
import com.sinodata.bsm.center.bean.Page;
import com.sinodata.bsm.center.cache.ProtocolParameterCache;
import com.sinodata.bsm.center.dao.impl.ProtocolParameterDaoImpl;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-11 上午9:52:43          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class ProtocolParameterService {

    @Autowired
    private ProtocolParameterCache protocolParameterCache;

    @Autowired
    private ProtocolParameterDaoImpl protocolParameterDao;

    /**
     * 分页查询采集协议
     * 
     * @param page
     * @param condition
     * @return
     */
    public Page<ProtocolParameter> findByPage(Page<ProtocolParameter> page, Condition condition) {
        return protocolParameterDao.findByPage(page, condition);
    }

    /**
     * 新增一条采集协议配置记录
     * 
     * @param protocolParameter
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(ProtocolParameter protocolParameter) {
        protocolParameterDao.save(protocolParameter);
    }

    /**
     * 修改一条采集协议配置记录
     * 
     * @param protocolParameter
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(ProtocolParameter protocolParameter) {
        protocolParameterDao.update(protocolParameter);
    }

    /**
     * 批量删除采集协议记录
     * 
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Long[] ids) {
        protocolParameterDao.batchDelete(ids);
    }

    /**
     * 通过ID查询
     * 
     * @param id
     * @return
     */
    public ProtocolParameter get(Long id) {
        return protocolParameterCache.get(id);
    }

    /**
     * 根据条件查询总数
     * 
     * @param condition
     * @return
     */
    public int getTotal(Condition condition) {
        return protocolParameterDao.getCountByCondtion(condition);
    }

    /**
     * 根据协议类型获取采集协议
     * @return
     */
    public List<ProtocolParameter> findByType(String type) {
        List<ProtocolParameter> paramers = protocolParameterCache.getAll();
        List<ProtocolParameter> returnParamers = new ArrayList<ProtocolParameter>();
        for (int i = 0; i < paramers.size(); i++) {
            ProtocolParameter parameter = paramers.get(i);
            if (parameter.getType().equals(type)) {
                returnParamers.add(parameter);
            }
        }
        return returnParamers;
    }
}